package com.xinyue.game.server.gamehandler;

import com.xinyue.game.messages.ChatMessagePush;
import com.xinyue.game.messages.ChatRequest;
import com.xinyue.game.messages.ChatResponse;
import com.xinyue.game.server.system.PlayerChannelService;
import com.xinyue.network.hamdlermapping.GameChannelContext;
import com.xinyue.network.hamdlermapping.GameHandlerComponent;
import com.xinyue.network.hamdlermapping.GameMapping;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 王广帅
 * @date 2021年02月03日 2:47 下午
 */
@GameHandlerComponent
public class ChatHandler extends AbstractGameHandler {
    @Autowired
    private PlayerChannelService playerChannelService;
    @GameMapping(ChatRequest.class)
    public void receiveChatRequest(GameChannelContext ctx, ChatRequest request){
        logger.debug("{} say:{}",ctx.getPlayerId(),request.getMessage());
        ChatResponse response = new ChatResponse();
        ctx.sendGameMessage(response);
        if(request.getChatType() == 1){
            //世界聊天，给所有人发送聊天消息
            ChatMessagePush chatMessagePush = new ChatMessagePush();
            chatMessagePush.setPlayerId(ctx.getPlayerId());
            //暂时自定义一下，后面加上用户数据库存储之后再修改。
            chatMessagePush.setName("Chat:" + ctx.getPlayerId());
            chatMessagePush.setMessage(request.getMessage());
            playerChannelService.broadcast(chatMessagePush);
        }
    }


}
